package com.tencreat.mall.controller.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.domain.mall.MallGoodsCut;
import com.tencreat.domain.mall.dto.*;
import com.tencreat.domain.mall.vo.*;
import com.tencreat.service.mall.IMallGoodsCutService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 砍价商品Controller
 *
 * @author wangkai
 * @date 2021-11-04
 */
@Api(tags={"5-商品砍价"})
//@RestController
@RequestMapping("/mobile/api/cut")
public class MallGoodsCutController extends BaseController {
    //@Autowired
    private IMallGoodsCutService mallGoodsCutService;

    /**
     * 查询砍价商品列表
     */
    @ApiOperation(value = "app端查询砍价商品列表")
    @ApiIgnore
    @PostMapping("/list")
    public HttpResult<IPage<ResMallGoodsCutsVO>> list(@RequestBody @Validated MallGoodsCutDTO mallGoodsCutDTO) {
        IPage<ResMallGoodsCutsVO> page = new Page<>(mallGoodsCutDTO.getPageNum(), mallGoodsCutDTO.getPageSize());
        return HttpResult.ok(mallGoodsCutService.page(page, mallGoodsCutDTO));
    }

    /**
     * 获取砍价商品详细信息
     */
    @ApiOperation(value = "app端获取砍价商品详细信息")
    @PostMapping(value = "/getInfo")
    public HttpResult<ResGoodsCutDetailVO> getInfo(@RequestBody ReqIdParam id) {
        return mallGoodsCutService.selectMallGoodsCutById(id, getLoginCheck());
    }


    /**
     * 开始砍价(废除)
     * 砍价合并到/api/goods/submit提交订单接口
     *
     * @return ResOrderMallSubmit
     */
    @PostMapping("/submit")
    @ApiOperation(value = "开始砍价(废除)")
    @ApiIgnore
    public HttpResult<ResOrderMallSubmitVO> submit(@Valid @RequestBody OrderMallSubmitDTO orderVO) {
        return mallGoodsCutService.submit(orderVO, getLoginCheck());
    }

    /**
     * 帮他砍价
     */
    @ApiIgnore
    @ApiOperation(value = "砍价")
    @Log(title = "砍价", businessType = BusinessType.OTHER)
    @PostMapping("/start")
    public HttpResult<ConcurrentHashMap<String, Object>> start(@Validated @RequestBody MallGoodsCutStartDTO startVO) {
        ConcurrentHashMap<String, Object> response = mallGoodsCutService.start(startVO);
        if (response.get("info") != null) {
            return HttpResult.error(String.valueOf(response.get("info")));
        } else {
            return HttpResult.ok(response);
        }
    }

    @ApiOperation("帮朋友砍价")
    @PostMapping("/bargain")
    HttpResult<BigDecimal> bargain(@RequestBody @Validated MallGoodsCutBargainDTO dto) {
        dto.initialize();
        BigDecimal money = mallGoodsCutService.bargain(dto);
        return HttpResult.ok(money);
    }

    /**
     * 注册登录之后领取砍价奖励(未注册不用调用)
     */
    @ApiOperation("领取砍价金额-旧")
    @PostMapping("/cutAfterReward")
    @ApiIgnore
    public HttpResult<?> cutAfterReward(@RequestBody @Validated CutAfterRewardDTO vo) {
        return HttpResult.ok(mallGoodsCutService.cutAfterReward(vo));
    }

    @ApiOperation("领取砍价金额-新")
    @PostMapping("/cutAfterReward2")
    public HttpResult cutAfterReward2(@NotBlank @ApiParam String unionId) {
        mallGoodsCutService.cutAfterReward2(unionId);
        return HttpResult.ok();
    }


    /**
     * 限时砍价列表
     */
    @ApiOperation("限时砍价个人信息")
    @GetMapping("/cutIngInfo")
    public HttpResult<ResCutIngInfoVO> cutIngInfo(@ApiParam(value = "砍价商品ID") @RequestParam Long cutId) {
        return mallGoodsCutService.cutIngInfo(cutId, getLoginCheck());
    }

    @ApiOperation("根据订单号获取限时砍价个人信息")
    @GetMapping("/cutIngInfo-order")
    public HttpResult<ResCutIngInfoVO> cutIngInfoOrder(@ApiParam(value = "订单编号") @RequestParam @NotBlank String orderSn) {
        return mallGoodsCutService.cutIngInfo(orderSn);
    }

    /**
     * 我的砍价列表
     */
    @ApiOperation("我的砍价列表")
    @PostMapping ("/myCutList")
    public HttpResult<IPage<ResMyCutInfosVO>> myCutList(@RequestBody MallCutListDTO dto) {
        Page<ResMallGoodsCutsVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        dto.setUuid(getLoginCheck().getAccountLogin().getUuid());
        return HttpResult.ok(mallGoodsCutService.myCutList(page,dto));
    }


    /**
     * 查看砍价订单信息
     */
    @ApiOperation("查看砍价订单信息")
    @GetMapping("/getCutOrderInfo")
    @ApiImplicitParam(value = "订单号", name = "orderSn")
    public HttpResult<CutOrderInfoVO> getCutOrderInfo(@NotBlank String orderSn) {
        return HttpResult.ok(mallGoodsCutService.getCutOrderInfo(orderSn));
    }
}
